<?php
require "../classes/ContactCli.class.php";
require "../classes/ContactAddress.class.php";
require "../classes/ClientsDAO.class.php";
require "../classes/ConnectionFactory.class.php";
include "\InsertAddress.action.php";
require "../classes/AddressDAO.class.php";
require"checksDate.action.php";

$date = new checksDate();
$cli = new ContactCli();
$ad=new ContactAddress();

$cadast = $date->dataAtual();

$cli->setId($_POST['idCli']);
$cli->setName($_POST['cliNameInput']);
$cli->setPhone($_POST['cliPhoneInput']);
$cli->setBirth($date->convertsDate($_POST['cliDateInput']));
$cli->setDoc($_POST['cliDocInput']);
$cli->setSex($_POST['cliGenreSelect']);
$cli->setDatCad($cadast);


$ad->setIdAddress($_POST['addresIdInput']);
$ad->setStreet($_POST['cliStreetInput']);
$ad->setNumber($_POST['cliNumbInput']);
$ad->setNeigh($_POST['cliNeigInput']);
$ad->setZipcod($_POST['cliZipInput']);
$ad->setCity($_POST['cliCityInput']);
$ad->setState($_POST['cliStateInput']);


$cf = new ConnectionFactory();
$db = $cf->getConnection();

$dao = new ClientsDAO($db);
$id = $cli->getId();
$daoAddres = new AddressDAO($db);


if (empty($id)) {
    $result = $dao->create($cli);
    echo "$result";

    $doc = $cli->getDoc();
    $newcli = $dao->getByDoc($doc);

    $idClient = $newcli->getId();
    $ad->setIdClients($idClient);
    $daoAddres->create($ad);
} else {
    $result = $dao->update($cli);
    $ad->setIdClients($id);
    $daoAddres->update($ad);
}

header("Location: ../index.php");
?>

<script>
        <meta http-equiv="content-type" content="text/html;charset=utf-8" />;
    var cadTest = <?php echo  $result; ?>;
           
    if(cadTest){
        alert("Cadastro Efetuado Com Sucesso!");
        setTimeout("document.location = '../index.php'",500);
       
    }else{
       
        alert("Não Foi Possível Realizar o Cadastro!");
        setTimeout("document.location = '../index.php'",500);
    }
    
             
</script>